<template>
  <div class="head-wrapper">
    <div class="head">
      <div class="g-box">
        <div class="nav">
          <div class="logo">
            <a href="/">
              <img src="@/assets/images/logo2.png" />
            </a>
          </div>
          <div class="menu">
            <div class="menu__item" v-for="(item, i) in list" :key="i">
              <a
                :href="item.path || 'javascript:void(0);'"
                :class="{ on: item.name === name }"
                v-if="!item.children"
                >{{ item.name }}</a
              >
              <div class="submenu" v-if="item.children">
                <a
                  :href="item.path || 'javascript:void(0);'"
                  :class="{ on: item.name === name }"
                  >{{ item.name }}</a
                >
                <div class="submenu__item">
                  <ul>
                    <li v-for="(subitem, j) in item.children" :key="j">
                      <a
                        :href="subitem.path"
                        :class="{
                          small: subitem.name === 'Trading Competition',
                        }"
                        >{{ subitem.name }}</a
                      >
                    </li>
                  </ul>
                </div>
              </div>
            </div>
          </div>
          <div class="side" v-if="name === 'home'">
            <a
              href="/swap"
              class="g-clickable launch-app"
            >
              <span>Launch APP</span>
            </a>
          </div>
          <div class="side" v-else>
            <div class="g-clickable button chain" @click="showChain">
              <img :src="chain.chainIcon" />
              <span class="name">{{ chain.chainName }}</span>
              <i class="iconfont icon-arrow-up"></i>
            </div>
            <div class="submenu">
              <div class="g-clickable button connect" @click="showAccount">
                <img src="@/assets/images/logo-small.png" />
                <span v-if="account">{{ account | formatAccount }}</span>
                <span v-else>Connect Wallet</span>
                <i class="iconfont icon-arrow-up"></i>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <NouDialog close-on-click-modal :visible="isShowConnect">
      <Connect @close="isShowConnect = false" />
    </NouDialog>
    <NouDialog close-on-click-modal :visible="isShowAccount">
      <Account @close="isShowAccount = false" />
    </NouDialog>
    <NouDialog close-on-click-modal :visible="isShowChain">
      <Chain @close="isShowChain = false" />
    </NouDialog>
  </div>
</template>

<script>
import Connect from "@/components/Connect";
import Account from "@/components/Account";
import Chain from "@/components/Chain";
import NouDialog from "@/components/common/NouDialog";
import { mapState } from "vuex";
import { getChainByCode } from "@/config/chain";

export default {
  components: {
    Connect,
    Account,
    Chain,
    NouDialog,
  },
  computed: {
    ...mapState({
      chainCode: (state) => state.chainCode,
      account: (state) => state.account,
      connect: (state) => state.connect,
      price: (state) => state.price,
    }),
    name() {
      return this.$route.name || "home";
    },
    chain() {
      return getChainByCode(this.chainCode);
    }
  },
  data() {
    return {
      isShowConnect: false,
      isShowChain: false,
      isShowAccount: false,
      list: [
        {
          name: "Payment",
          path: "/payment"
        },
        {
          name: "Airdrop",
          path: "/airdrop"
        },
        {
          name: "Claim",
          path: "/claim"
        },
        {
          name: "Prices",
          path: "/price"
        },
        {
          name: "Telegram Bot",
          path: "https://t.me/my_boss_pay_bot"
        },
        {
          name: "Dashboard",
          path: "/dashboard"
        },
      ],
    };
  },
  methods: {
    showAccount() {
      if (this.account) {
        this.isShowAccount = true;
      } else {
        this.isShowConnect = true;
      }
    },
    showChain() {
      this.isShowChain = true;
    }
  },
};
</script>

<style lang="scss" scoped>
.head-wrapper {
  position: relative;
  z-index: 99999;
  &.head-perpetual {
    .head {
      .g-box {
        width: initial;
        margin: 0 15px;
      }
    }
  }
  &.head-nft-creator {
    .head {
      backdrop-filter: initial;
      .g-box {
        width: initial;
        margin: 0 15px;
      }
    }
  }
}
.head {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  backdrop-filter: blur(20px);
  /* background-color: var(--background); */
  z-index: 9999;

  .nav {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
  }

  .logo {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    img {
      width: 200px;
    }
  }

  .menu {
    height: 72px;
    display: flex;
    align-items: center;
    justify-content: center;

    &__item {
      font-size: 16px;
      font-family: "Roboto Medium";
      padding: 0 20px;
      margin: 0 5px;
      position: relative;

      a {
        color: var(--color-content);
        text-decoration: none;
        display: block;
        &.small {
          font-size: 14px;
        }
        &.on,
        &:hover {
          color: var(--color);
        }
      }

      &:hover {
        .menu-sub {
          max-height: initial;
          overflow: auto;
        }
      }

      .submenu {
        .iconfont {
          margin: 5px 0 0 5px;
          display: inline-block;

          transform: scale(0.8) rotate(180deg);
          transition: all 0.3s ease;
          color: var(--color);
          opacity: 0.3;

          &:hover {
            .iconfont {
              transform: scale(0.8) rotate(0deg);
            }
          }
        }
      }
    }
  }

  .submenu {
    position: relative;
    display: flex;
    align-items: center;

    &__item {
      position: absolute;
      left: 50%;
      top: 100%;
      padding-top: 10px;
      min-width: 150px;
      transform: translateX(-50%);
      max-height: 0;
      overflow: hidden;
      transition: all 0.3s ease;
      transition-delay: 0.2s;
    }

    &:hover {
      & > a {
        color: var(--color);
      }
      .submenu__item {
        max-height: initial;
        ul {
          &::after {
            display: block;
          }
        }
      }
    }

    ul {
      padding: 12px 0;
      border-radius: 12px;
      background-color: var(--color-grey);
      position: relative;
      &::after {
        content: "";
        position: absolute;
        top: -10px;
        left: 50%;
        transform: translateX(-50%);
        border-width: 5px;
        border-style: solid;
        border-color: transparent transparent var(--color-grey) transparent;
        display: none;
      }
      li {
        text-align: center;
        line-height: 50px;
        cursor: pointer;
        span {
          font-size: 13px;
          font-family: "Roboto Medium";
          color: var(--color-content);
        }
        &:hover {
          background-color: rgba(255, 255, 255, 0.05);
          a,
          span {
            color: var(--color);
          }
        }
      }
    }
  }

  .side {
    display: flex;
    align-items: center;
    justify-content: flex-end;

    .button {
      padding: 0 12px;
      height: 46px;
      border-radius: 46px;
      background: var(--card);
      box-sizing: border-box;

      display: flex;
      align-items: center;
      justify-content: center;

      img {
        width: 28px;
        border-radius: 14px;
        box-sizing: border-box;

        margin-right: 7px;
      }

      span {
        font-size: 14px;
        font-family: "Roboto Bold";
        display: inline-block;
        white-space: nowrap;
        color: var(--color-content);
      }

      .iconfont {
        font-size: 20px;
        font-weight: 600;
        transform: rotate(180deg) scale(0.8);
        color: var(--color-content);
        margin-left: 6px;
        transition: all 0.3s ease;
      }
    }

    .launch-app {
      cursor: pointer;
      width: 120px;
      height: 40px;
      box-sizing: border-box;
      border-radius: 20px;
      margin-left: 20px;
      text-decoration: none;

      display: flex;
      align-items: center;
      justify-content: center;

      background-image: linear-gradient(
        90deg,
        #553b7c 0%,
        #795dc6 91.89%
      );

      span {
        font-size: 14px;
        font-family: "Roboto Medium";
        color: var(--color);
      }
    }

    .submenu {
      margin-left: 25px;
    }
  }
}
</style>
